package com.example.wechat_b.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wechat_b.R;
import com.example.wechat_b.activity.FindActivity;
import com.example.wechat_b.activity.HealthActivity;
import com.example.wechat_b.adapter.FindRecycleViewAdapter;
import com.example.wechat_b.bean.FindRecycleBean;
import com.example.wechat_b.utils.RecycleViewItemListener;
import com.example.wechat_b.utils.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FindFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FindFragment extends Fragment implements RecycleViewItemListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private TextView title;
    private RecyclerView recyclerView;
    private List<FindRecycleBean> beans;
    private FindRecycleViewAdapter adapter;

    public FindFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FindFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FindFragment newInstance(String param1, String param2) {
        FindFragment fragment = new FindFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        title = view.findViewById(R.id.title_text);
        recyclerView = view.findViewById(R.id.find_recycle);
        beans = new ArrayList<>();
        beans = getData();
        init();
        return view;
    }

    private void init() {
        title.setText("发现");
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
//        SpaceItemDecoration decoration = new SpaceItemDecoration(8);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new SpaceItemDecoration(SpaceItemDecoration.FIND_RECYCLE_DECORATION,40));
        /**
        * 2021.11.5 00：00
         * 更新recycle view后需要另一个参数（监听接口）
         * 在其adapter类当中 可选择性注释需要此监听器接口的构造器 注销后程序无误
         * 若设置监听器参数后应立刻实现监听接口
        * */
        adapter = new FindRecycleViewAdapter(beans,this);
        recyclerView.setAdapter(adapter);
    }

    private List<FindRecycleBean> getData() {
        FindRecycleBean bean1 = new FindRecycleBean();
        bean1.setLeftImg(R.drawable.ic_find_friendround);
        bean1.setName(getResources().getString(R.string.pyq));
        bean1.setTip("有新的动态");

        FindRecycleBean bean2 = new FindRecycleBean();
//        bean2.setLeftImg(R.drawable.ic_find_friendround);
        bean2.setName("视频号");

        FindRecycleBean bean3 = new FindRecycleBean();
        bean3.setLeftImg(R.drawable.ic_scanhand);
        bean3.setName("扫一扫");

        FindRecycleBean bean4 = new FindRecycleBean();
        bean4.setLeftImg(R.drawable.ic_shake);
        bean4.setName("摇一摇");

        FindRecycleBean bean5 = new FindRecycleBean();
        bean5.setLeftImg(R.drawable.ic_see);
        bean5.setName("看一看");

        FindRecycleBean bean6 = new FindRecycleBean();
        bean6.setLeftImg(R.drawable.ic_search);
        bean6.setName("搜一搜");

        FindRecycleBean bean7 = new FindRecycleBean();
        bean7.setLeftImg(R.drawable.ic_live);
        bean7.setName("直播和附近");

        FindRecycleBean bean8 = new FindRecycleBean();
        bean8.setLeftImg(R.drawable.ic_shopingpackage);
        bean8.setName("购物");

        FindRecycleBean bean9 = new FindRecycleBean();
        bean9.setLeftImg(R.drawable.ic_game);
        bean9.setName("游戏");

        FindRecycleBean bean10 = new FindRecycleBean();
        bean10.setLeftImg(R.drawable.ic_program);
        bean10.setName("小程序");

        FindRecycleBean bean11 = new FindRecycleBean();
        bean11.setLeftImg(R.drawable.ic_health);
        bean11.setName("健康");

        beans.add(bean1);
        beans.add(bean2);
        beans.add(bean3);
        beans.add(bean4);
        beans.add(bean5);
        beans.add(bean6);
        beans.add(bean7);
        beans.add(bean8);
        beans.add(bean9);
        beans.add(bean10);
        beans.add(bean11);
        return beans;
    }

    @Override
    public void onItemClick(View v, int position) {
        switch (position){
            case 0:
                Intent intent = new Intent(getActivity(), FindActivity.class);
                getActivity().startActivity(intent);
                break;
            case 10:
                getActivity().startActivity(new Intent(getActivity(), HealthActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemLongClick() {

    }
}